package com.c.ctgproject.ui.collect.check

import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.adapter.guang.CheckListAdapter
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityPassCheckBinding
import com.c.ctgproject.entity.guang.CheckListCommonEntity
import com.c.ctgproject.event.PositionEvent
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.*
import org.greenrobot.eventbus.Subscribe

/**
 *作者:LiBW
 *创建日期: 2021/10/12
 *描述: 放行审批 页面
 */
class PassCheckActivity : BaseActivityV2(true) {
    private val mBinding by lazy { ActivityPassCheckBinding.inflate(layoutInflater) }
    override fun getLayout() = mBinding.root
    private var page = 1
    private val mList = ArrayList<CheckListCommonEntity.DataBean.ListBean>()
    private val mAdapter by lazy { CheckListAdapter(this, mList) }

    @Subscribe
    fun event(type: PositionEvent) {
        if (type.position != -1) {
            mList.removeAt(type.position)
            mAdapter.notifyItemRemoved(type.position)
        }
    }

    override fun event() {
        getData()
        mBinding.mRecyclerview.adapter = mAdapter
        mBinding.mRecyclerview.layoutManager = LinearLayoutManager(this)
    }

    private var search: String = ""

    override fun click() {
        mBinding.mSearchET.handlerText {
            if (it.isEmpty() || it == "") {
                search = ""
                page = 1
                getData()
            }
        }
        mBinding.mSearchTV.setOnClickListener {
            search = mBinding.mSearchET.text.handlerNull()
            page = 1
            getData()
        }


        mBinding.mTabLayout.handlerSelectReturnText {
            when (it) {
                "待审批" -> {
                    page = 1
                    status = 1
                    getData()
                }
                "已审批" -> {
                    page = 1
                    status = 2
                    getData()
                }
            }
        }
        mBinding.mRefresh.setOnLoadMoreListener {
            page++
            getData()
        }

        mBinding.mRefresh.setOnRefreshListener {
            page = 1
            getData()
        }
    }

    //放行条状态 0-新建 1-待审批 2-已审批
    private var status = 1

    private fun getData() {
        if (status == 1) {
            ApiMethods.apiSubscribe(ApiMethods.retrofit.releaseStripListMyAudit(search, page), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
                MyLog.d("待审批回调:$it")
                handlerData(it)
            })
        } else {
            ApiMethods.apiSubscribe(ApiMethods.retrofit.releaseStripListMyAuditFinish(search, page), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
                MyLog.d("已审批回调:$it")
                handlerData(it)
            })
        }

//        ApiMethods.apiSubscribe(ApiMethods.retrofit.releaseStripList(search, page, status), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
//            MyLog.d("放行条列表:$it")
//            val data = getGson().fromJson(it, CheckListCommonEntity::class.java).data
//            data?.list?.let { list ->
//                if (page == 1) mList.clear()
//                mList.addAll(list)
//                mAdapter.notifyDataSetChanged()
//            }
//        })
    }

    private fun handlerData(it: String) {
        val data = getGson().fromJson(it, CheckListCommonEntity::class.java).data
        data?.list?.let { list ->
            if (page == 1) mList.clear()
            mList.addAll(list)
            mAdapter.notifyDataSetChanged()
            mBinding.mRecyclerview.handlerPosition(page)
        }
    }
}